package com.bq.gametrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.TradingRecordsDetailsAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.TradingRecordsDetails;
import com.bq.gametrade.bean.TradingRecordsDetailsBean;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.utils.ToastUtils;
import com.bq.gametrade.widget.PromptAlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class TradingRecordsDetailsActivity extends BaseActivity implements View.OnClickListener{

    private ImageView back,ivService;
    private TextView tvTitle;
    private ListView listView;
    private ArrayList<TradingRecordsDetailsBean> mList = new ArrayList<>();
    private TradingRecordsDetailsAdapter adapter;
    private Activity activity;
    private String tradingRecordId = "",tradingType = "";
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tradingrecords_details);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        activity = this;
        tradingRecordId = getIntent().getStringExtra("tradingRecordId");
        tradingType = getIntent().getStringExtra("tradingType");
        initView();
        getTradingRecordDetails();
    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("交易记录详情");
        listView = serchViewById(R.id.lv_tradingRecords_details);
        adapter = new TradingRecordsDetailsAdapter(activity,mList,tradingType);
        listView.setAdapter(adapter);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
        }
    }

    private void getTradingRecordDetails(){
        if(tradingRecordId.equals("")){
           toastShow("获取交易记录详情失败请重试");
            finish();
            return;
        }
        Map<String,String> params = new HashMap<>();
        params.put("token",userInfo.token);
        params.put("orderId",tradingRecordId);
         VRHttp.getTradingRecordsDeatilsList(TradingRecordsDetailsActivity.this, HttpLink.TRADINGTRCORD_DETAILS_URL, params, new VRHttpListener() {
             @Override
             public void onSuccess(Object response, boolean isCache) {
                 if(response != null){
                     TradingRecordsDetails details = (TradingRecordsDetails)response;
                     if(details.status.equals("0")){
                         mList = details.record_list;
                         adapter.setData(mList,tradingType);
                         adapter.notifyDataSetChanged();
                     }else if(details.status.equals("99")){
                         ToastUtils.showToast(activity,"登录超时请重新登陆");
                         application.setUserInfo(null);
                         startActivity(new Intent(activity,LoginActivity.class));
                     }else if(details.status.equals("98")){
                         application.setUserInfo(null);
                         new PromptAlertDialog(activity).builder().setTitle("提示").setTouchOutsideCanceled(true)
                                 .setMsg("您的账号已在其他设备登录，如果不人本人登录请及时修改密码")
                                 .setPositiveButton("修改密码", new View.OnClickListener() {
                                     @Override
                                     public void onClick(View v) {
                                         startActivity(new Intent(activity, RetrievePasswordActivity.class));
                                     }
                                 }).setNegativeButton("取消", new View.OnClickListener() {
                             @Override
                             public void onClick(View v) {
                                 startActivity(new Intent(activity,LoginActivity.class));
                             }
                         }).show();

                     }
                 }
             }
         });
    }
}
